﻿#include <string.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <map>

class ParameterReader
{
public:
    ParameterReader( std::string filename="./Default.cfg" )
    {
        data.clear();
        std::ifstream fin( filename.c_str() );
        if (!fin)
        {
            std::cerr<<"parameter file does not exist.\n";
            return;
        }
        while(!fin.eof())
        {
            std::string str;
            std::getline( fin, str );
            if (str[0] == '#')
            {
                continue;
            }

            int pos = str.find("=");
            if (pos == -1)
                continue;
            std::string key = str.substr( 0, pos );
            std::string value = str.substr( pos+1, str.length() );
            data[key] = value;

            if ( !fin.good() )
                break;
        }
        fin.close();
    }

    ~ParameterReader(){
        data.clear();
    }

    bool getData( std::string key,std::string &value)
    {
        std::map<std::string, std::string>::iterator iter = data.find(key);
        if (iter == data.end())
        {
            return false;
        }
        value = iter->second;
        return true;
    }

private:
    std::map<std::string, std::string> data;
};
